package com.ztgf.order.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.order.common.config.YmlConfig;
import com.ztgf.order.mapper.OrderRightsProtectionMapper;
import com.ztgf.order.models.bo.OrderRightsProtectionBO;
import com.ztgf.order.models.bo.OrderRightsProtectionSaveBO;
import com.ztgf.order.models.dto.OrderInfoDTO;
import com.ztgf.order.models.dto.OrderRightsProtectionDTO;
import com.ztgf.order.models.service.IOrderInfoService;
import com.ztgf.order.models.service.IOrderRightsProtectionService;
import com.ztgf.order.models.service.IOrderStatusService;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;

/**
 * 商家维权表控制器
 *
 * @author YUE
 * @Date 2020-06-29 15:16:03
 */
@DubboService
@Lazy
public class OrderRightsProtectionServiceImpl extends ServiceImpl<OrderRightsProtectionMapper, OrderRightsProtectionDTO> implements IOrderRightsProtectionService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    @Lazy
    private IOrderStatusService orderStatusService;

    @Autowired
    @Lazy
    private IOrderInfoService orderInfoService;

    @DubboReference
    private IShopInfoService shopInfoService;

    @Override
    public boolean add(OrderRightsProtectionSaveBO saveBO) {
        OrderRightsProtectionDTO dto = new OrderRightsProtectionDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public boolean addByOrderIdAndType(Long orderId, int type,String reason) {
        OrderRightsProtectionDTO dto = new OrderRightsProtectionDTO();
        dto.setReason(reason);
        OrderInfoDTO orderInfoDTO = orderInfoService.selectById(orderId);
        dto.setOrderId(orderId);
        dto.setInitiator(type);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        Long buyShopId = orderInfoDTO.getBuyShopId();
        ShopInfoDTO buyShopInfo = shopInfoService.selectById(buyShopId);
        dto.setBuyerName(buyShopInfo.getName());//买家公司名称
        Long sellShopId = orderInfoDTO.getSellShopId();
        ShopInfoDTO sellShopInfo = shopInfoService.selectById(sellShopId);
        dto.setSellerName(sellShopInfo.getName());//卖家公司名称
        dto.setOrderNo(orderInfoDTO.getOrderNo());//订单编号
        return this.insert(dto);
    }

    /**
     * 更新审核结果
     *
     * @param orderRightsProtectionBO
     * @return
     */
    @Override
    public boolean updateResultById(OrderRightsProtectionBO orderRightsProtectionBO) {
        OrderRightsProtectionDTO orderRightsProtectionDTO = selectById(orderRightsProtectionBO.getId());
        if (orderRightsProtectionDTO == null) {
            return false;
        }
        orderRightsProtectionDTO.setResult(orderRightsProtectionBO.getResult());
        boolean b = updateById(orderRightsProtectionDTO);//更新维权表中的信息
        if (!b) {
            return false;
        }
        //修改订单状态中的维权信息
        if (orderRightsProtectionBO.getResult() == 1) {
            return orderStatusService.updateRightsProtection(orderRightsProtectionDTO.getOrderId(), orderRightsProtectionDTO.getInitiator(), 2);
        }
        return orderStatusService.updateRightsProtection(orderRightsProtectionDTO.getOrderId(), orderRightsProtectionDTO.getInitiator(), 3);
    }
}
